<template>
  <el-col class="app-v-center app-column" style="overflow-y: auto;">
    <el-row class="app-h-48px app-fill-width app-v-center pl-4 app-bb-1px-l2" style="background: #dfe9f5" :gutter="10">
      <el-button
        size="mini"
        :disabled="role>1"
        @click="toAddUser()"
      >添加用户</el-button>
      <!-- <el-button
        size="mini"
      >删除</el-button> -->
    </el-row>
    <el-table
      :data="userList"
      class="app-flex-1"
      style="width: 100%;overflow-y: auto;"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
      /> -->
      <el-table-column
        prop="username"
        align="center"
        label="用户名"
        width="180"
      />
      <el-table-column
        prop="name"
        align="center"
        label="昵称"
        width="180"
      />
      <el-table-column
        prop="mobile"
        align="center"
        label="电话"
        width="180"
      />
      <el-table-column
        prop="role"
        align="center"
        label="角色权限"
        width="180"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.role === 1 ? '主管': scope.row.role === 2 ? '活动负责人': '奖品专员' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="添加时间"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.createTime.replace('T',' ') }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-if="role===1"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mb-10 mt-5"
      background
      :current-page.sync="pageNo"
      :page-sizes="[10, 20, 50]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getUserList"
      @current-change="getUserList"
    />
    <el-dialog
      :title="isEdit? '添加用户' : '修改用户'"
      :visible.sync="addUserDialog"
      width="50vw"
    >
      <el-form ref="form" :rules="rules" class="app-width-80p" :model="form" label-width="160px">
        <el-form-item prop="name" label="用户姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item prop="mobile" label="用户电话">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item prop="username" label="用户账号">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item prop="password" label="用户密码">
          <el-input v-model="form.password" show-password />
        </el-form-item>
        <el-form-item prop="role" label="用户角色">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="主管" :value="1" />
            <el-option label="活动负责人" :value="2" />
            <el-option label="奖品专员" :value="3" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addUserDialog = false">取 消</el-button>
        <el-button type="primary" @click="addUser()">确 定</el-button>
      </div>
    </el-dialog>
  </el-col>
</template>

<script>
import { getUserList,updateUser,deleteUserById,addUser } from '@/api/org-user'
export default {
  data: () => ({
    rules: {
      name: [
        { required: true, message: '请输入活动名称', trigger: 'blur' }
      ],
      mobile: [
        { required: true, message: '请输入用户电话', trigger: 'blur' }
      ],
      username: [
        { required: true, message: '请输入用户账号', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入用户密码', trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择用户角色', trigger: 'blur' }
      ]
    },
    isEdit: false,
    role: Number(JSON.parse(localStorage.getItem('userInfo')).role),
    addUserDialog: false,
    userList: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    form: {
      name: '',
      mobile: '',
      username: '',
      password: '',
      role: null
    }
  }),
  mounted() {
    this.getUserList()
  },
  methods: {
    toAddUser() {
      this.form = {
        name: '',
        mobile: '',
        username: '',
        password: '',
        role: null
      }
      this.isEdit = false
      this.addUserDialog = true
    },
    getUserList() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        name: this.name
      }
      getUserList(params).then(res => {
        if (res.code === 200) {
          this.userList = res.data.dataList
          this.total = res.data.total
        }
      })
    },
    handleEdit(index,row) {
      this.isEdit = true
      this.form = row
      this.addUserDialog = true
    },
    handleDelete(index,row) {
      this.$confirm(`确认删除用户${row.name}？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserById({ id: row.id }).then(res => {
          if (res.code === 200) {
            this.$message({ type: 'success',message: '已删除' })
            this.pageNo = 1
            this.getUserList()
          }
        })
      }).catch(() => {
        this.$message('已取消')
      })
    },
    addUser() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateUser(this.form).then(res => {
              if (res.code === 200) {
                this.getUserList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addUserDialog = false
              }
            })
          } else {
            addUser(this.form).then(res => {
              if (res.code === 200) {
                this.pageNo = 1
                this.getUserList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addUserDialog = false
              }
            })
          }
        }
      })
    }
  }
}
</script>
